package cn.itheima.web;


import cn.itheima.domain.User;
import cn.itheima.service.UserService;
import cn.itheima.service.UserServiceImp;
import cn.itheima.utils.CommonsUtils;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2018/4/12.
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String,String[]> parameterMap=request.getParameterMap();
        String checkCode=request.getParameter("checkCode");
        HttpSession session=request.getSession();
        Map<String,String> err=new HashMap<String ,String>();
        User user=new User();
        request.setAttribute("err",err);
        request.setAttribute("user",user);

        try {
            ConvertUtils.register(new Converter() {
                @Override
                public Object convert(Class aClass, Object o) {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    Date date=null;
                    try {
                        date=sdf.parse(o.toString());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return date;
                }
            }, Date.class);
            BeanUtils.populate(user,parameterMap);
            if(checkCode==null||"".equals(checkCode)){
                err.put("checkCode","请输入验证码");

            }
            String uid=CommonsUtils.getUUID();
            user.setUid(uid);
            user.setState(1);
            user.setStartDate(new Date().toLocaleString());

            user.setCode(uid);
            user.setRoot(1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserService rs=new UserServiceImp();
        boolean isRegisterSuccess=rs.register(user);
        if(isRegisterSuccess){
            String mailMsg=user.getUname()+"欢迎您注册heimashop<a href=http://localhost:8080/"+request.getContextPath()+"/active?activeCode="+user.getCode()+">点击激活</a>";
//            try {
//                MailUtils.sendMail(user.getEmail(),mailMsg);
//            } catch (MessagingException e) {
//                e.printStackTrace();
//            }
            //response.sendRedirect(request.getContextPath()+"/registerSuccess.jsp");
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/registerFail.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
